package com.eightgroups.tces.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.eightgroups.common.result.JsonResult;
import com.eightgroups.common.utils.StringUtil;
import com.eightgroups.tces.entity.Dept;
import com.eightgroups.tces.entity.Qtype;
import com.eightgroups.tces.service.IQtypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import com.eightgroups.common.controller.BaseController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 问卷 前端控制器
 * </p>
 *
 * @author zzccn
 * @since 2017-11-13
 */
@Controller
@RequestMapping("/admin/qtype")
public class QtypeController extends BaseController {
    @Autowired
    private IQtypeService IQtypeService;
    /**
     * 问卷列表页面
     * @return
     */
    @GetMapping("/list")
    public String list() {
        return "/admin/qtype/list";
    }

    /**
     * 问卷指标列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/getList")
    public Map<String, Object> getUserList(int pageNumber, int pageSize, String searchText) {
        Map<String,Object> result = new HashMap<String,Object>();
        Page<Qtype> page = new Page<>(pageNumber, pageSize);
        EntityWrapper<Qtype> wrapper = new EntityWrapper<>();
        wrapper.orderBy("id", true);
        Page<Qtype> qtypes = IQtypeService.selectPage(page,wrapper);
        result.put("total", qtypes.getTotal());
        result.put("rows", qtypes.getRecords());
        return result;
    }

    /**
     * 添加问卷
     * @param qtype
     * @return
     */
    @RequestMapping("/addQtype")
    @ResponseBody
    public JsonResult addQtype(Qtype qtype){
        return IQtypeService.insert(qtype)?renderSuccess("添加成功"):renderError("添加失败");
    }
    /**
     * 修改问卷
     * @param qtype
     * @return
     */
    @RequestMapping("/updateQtype")
    @ResponseBody
    public JsonResult updateDept(Qtype qtype){
        return IQtypeService.updateById(qtype)?renderSuccess("修改成功"):renderError("修改失败");
    }
    /**
     * 删除问卷
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(@RequestParam(value = "id", required = false) String id) {
        if (StringUtil.isEmpty(id)) {
            return renderError("请选择数据");
        }
        return IQtypeService.deleteQtypeByIdAndIndicator(id) ? renderSuccess("删除成功") : renderError("删除失败");
    }

}
